package GameEngine;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.*;

public abstract class GameEngine extends JFrame {

	private MouseEvent mouseevent;
	private KeyEvent keyevent;
	private static int frameWidth;
	private static int frameHeight;
	public static boolean rightKeyPressed, leftKeyPressed, upKeyPressed,
			downKeyPressed, pPressed, umPressed, iPressed = false;
	public static double fps;
	public static boolean pause = false;

	public abstract void init();

	public abstract void update();

	public GameEngine(String nome, int frameWidth, int frameHeight) {
		super(nome);
		GameEngine.frameHeight = frameHeight;
		GameEngine.frameWidth = frameWidth;
		this.initWindow();
		this.init();
		long getTime = 0;
		long tempoSegundo = 0;
		long iteracoes = 0;

		while (true) {
			// System.out.printf("\nfps = %.2f\n", GameEngine.fps);
			getTime = System.currentTimeMillis();
			this.update();
			this.repaint(0, 0, this.getWidth(), this.getHeight()); // testando
			if (GameObject2D.gameObjects != null) {
				for (int cont = 0; cont < GameObject2D.gameObjects.size(); cont++) {

					if (GameObject2D.gameObjects.get(cont).delete == true) {
						GameObject2D.gameObjects.remove(cont);
					} else {
						if (keyevent != null)
							GameObject2D.gameObjects.get(cont).updateKeyEvent(
									keyevent);
						if (mouseevent != null)
							GameObject2D.gameObjects.get(cont)
									.updateMouseEvent(mouseevent);
					}
				}
			}

			this.pause(16);

			GameEngine.fps = (1000D / ((double) System.currentTimeMillis() - (double) getTime));
			tempoSegundo += (System.currentTimeMillis() - getTime);
			iteracoes++;
			if (tempoSegundo >= 1000) {
				// System.out.println("fps medio = " + iteracoes);
				tempoSegundo = 0;
				iteracoes = 0;
			}
		}

	}

	public static int getFrameWidth() {
		return frameWidth;
	}

	public static int getFrameHeight() {
		return frameHeight;
	}

	private void pause(long TempoMiliSeconds) {
		try {
			Thread.sleep(TempoMiliSeconds);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void clearObjects() {
		if (GameObject2D.gameObjects != null)
			GameObject2D.gameObjects.clear();
	}

	private void initWindow() {
		MyComponent comp = new MyComponent();
		this.getContentPane().add(comp);
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(this.frameWidth, this.frameHeight);
		this.setVisible(true);

		addMouseListener(new MouseListener() {
			public void mousePressed(MouseEvent e) {
				mouseevent = e;
			}

			public void mouseReleased(MouseEvent e) {
				mouseevent = e;
			}

			public void mouseEntered(MouseEvent e) {
				mouseevent = e;
			}

			public void mouseExited(MouseEvent e) {
				mouseevent = e;
			}

			public void mouseClicked(MouseEvent e) {
				mouseevent = e;
			}
		});
		addMouseMotionListener(new MouseMotionListener() {

		
			public void mouseDragged(MouseEvent e) {
				mouseevent = e;
			}

			
			public void mouseMoved(MouseEvent e) {
				mouseevent = e;
			}
		});
		addKeyListener(new KeyListener() {

			public void keyTyped(KeyEvent e) {
				keyevent = e;
			}

			public void keyReleased(KeyEvent e) {
				keyevent = e;
			}

			public void keyPressed(KeyEvent e) {
				keyevent = e;
			}
		});
		addKeyListener(new KeyAdapter() {

			public void keyReleased(KeyEvent e) {
				if (e.getID() == KeyEvent.KEY_RELEASED) {
					if (e.getKeyCode() == KeyEvent.VK_UP) {
						upKeyPressed = false;
					}
					if (e.getKeyCode() == KeyEvent.VK_DOWN) {
						downKeyPressed = false;
					}
					if (e.getKeyCode() == KeyEvent.VK_LEFT) {
						leftKeyPressed = false;
					}
					if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
						rightKeyPressed = false;
					}
					if (e.getKeyChar() == 'p') {
						pPressed = false;
					}
					if (e.getKeyChar() == '1') {
						umPressed = false;
					}
					if (e.getKeyChar() == 'i') {
						iPressed = false;
					}
				}
			}

			public void keyPressed(KeyEvent e) {
				if (e.getID() == KeyEvent.KEY_PRESSED) {
					if (e.getKeyCode() == KeyEvent.VK_UP) {
						upKeyPressed = true;
					}
					if (e.getKeyCode() == KeyEvent.VK_DOWN) {
						downKeyPressed = true;
					}
					if (e.getKeyCode() == KeyEvent.VK_LEFT) {
						leftKeyPressed = true;
					}
					if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
						rightKeyPressed = true;
					}
					if (e.getKeyChar() == 'p') {
						pPressed = true;
					}
					if (e.getKeyChar() == '1') {
						umPressed = true;
					}
					if (e.getKeyChar() == 'i') {
						iPressed = true;
					}
				}
			}
		});
	}

}
